﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BioWeb.movieservice;
using System.Data.SqlClient;


namespace BioWeb
{
    public partial class ManageMovies : System.Web.UI.Page
    {
        MovieService movies;
        Dictionary<string, string> tempVars;
        protected void Page_Load(object sender, EventArgs e)
        {
            movies = new movieservice.MovieService();
            
            tempVars = new Dictionary<string, string>();
            
            foreach (BioWeb.movieservice.Movie m in movies.FindAllMovies())
            {
                tempVars.Add(m.title, m.serialNo);
                ListBox1.Items.Add(m.title);
            }
        }
       
        protected void populateLists()
        {
            ListBox1.Items.Clear();
            tempVars.Clear();
            foreach (BioWeb.movieservice.Movie m in movies.FindAllMovies())
            {
                tempVars.Add(m.title, m.serialNo);
                ListBox1.Items.Add(m.title);
            }
        }
        protected void DeleteMovie (object sender, EventArgs e)
        {
            try
            {
                string serialNo = (string)tempVars[ListBox1.SelectedItem.Text];
                movies.DeleteMovie(serialNo);
                populateLists();
            }
            catch (Exception ex)
            {
                Response.Write("Der er opstået en fejl: " + ex.Message);
            }
            
        }
        protected void CreateMovie(object sender, EventArgs e)
        {
            try
            {
                movies.CreateMovie(txtSerialNo.Text, txtLenght.Text, txtTitle.Text, txtGenre.Text, DateTime.Parse(txtReleaseDate.Text), true, Convert.ToDouble(txtRanking.Text), true);
                populateLists();
            }
            catch (Exception ex)
            {
                Response.Write("Der er opstået en fejl: " + ex.Message);
            }
        }

    }
}